package hazesoft.Collection;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class Collection_Upload extends Activity {
	
	String pathToOurFile = "/data/file_to_send.mp3";
	String urlServer = "http://www.hazeav.co.za/upload.aspx/";
	String lineEnd = "\r\n";
	String twoHyphens = "--";
	String boundary =  "*****";
	String PID = "NULL";
	
	int bytesRead, bytesAvailable, bufferSize;
	byte[] buffer;
	int maxBufferSize = 1*1024*1024;
	List<String> Files;
	
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Files =  new ArrayList<String>();
        setContentView(R.layout.upload);
        Button btnUpload = (Button)findViewById(R.id.btnUpload);
        btnUpload.setOnClickListener(btnUpload_OnClick);
        urlServer = GetPref("SERVER_ADDRESS");
        PID = this.getPreferences(MODE_PRIVATE).getString("PHONE_ID", null);
        
        TextView txtUpload = (TextView)findViewById(R.id.edtSummary);
        txtUpload.setTextSize(12);
        
        for(int x = 0; x <  fileList().length; x++)
        {
        	if(fileList()[x].contains(".dat"))
        	{
        		Files.add(fileList()[x]);
        	}
        }
        
        
        txtUpload.append("There is " + Files.size() + " file(s) waiting to be uploaded\n");
        if(Files.size() == 0)
        	btnUpload.setEnabled(false);
        
        for(int x = 0; x < Files.size(); x++)
        {
        	txtUpload.append("*" + Files.get(x) + "\n");
        }
      
    }
	
	public String GetPref(String Key)
	{
		return this.getSharedPreferences("AHB",MODE_PRIVATE).getString(Key, null);
	}
	
	String UploadFile(String Filename)
	{
		MessageBox("URL is " + urlServer);
		HttpURLConnection connection = null;
		connection.setConnectTimeout(20000);
		DataOutputStream outputStream = null;
		DataInputStream inputStream = null;
		String HTTP_Request = "";
		try
		{
			
			FileInputStream fIn = openFileInput(Filename);
			InputStreamReader in = new InputStreamReader(fIn);
			BufferedReader br = new BufferedReader(in);
			
			HTTP_Request +=	"?BarcodeID=" + br.readLine();
			HTTP_Request +=	"&SiteID=" + br.readLine();
			HTTP_Request += "&SwapBarcode=" + br.readLine();
				    
			HTTP_Request +=	"&Remarks=" + br.readLine();
			HTTP_Request +=	"&Rain=" + br.readLine();
			HTTP_Request +=	"&SupersCount=" + br.readLine();
			HTTP_Request +=	"&Bees=" + br.readLine();
			HTTP_Request +=	"&Capped=" + br.readLine();
			HTTP_Request += "&Fed=" + br.readLine();
				    
			HTTP_Request +=	"&Harvest=" + br.readLine();
			HTTP_Request +=	"&Manipulation=" + br.readLine();
			HTTP_Request +=	"&Disease=" + br.readLine();
			HTTP_Request +=	"&Queenright=" + br.readLine();
			HTTP_Request +=	"&GPS=" + br.readLine();
			HTTP_Request += "&PID=" + PID;
			HTTP_Request += "&Type=Upload";
			in.close();	    
		    
			HttpClient httpclient = new DefaultHttpClient();
		    HttpResponse response = httpclient.execute(new HttpGet(urlServer + HTTP_Request));
		    ByteArrayOutputStream out = new ByteArrayOutputStream();
			response.getEntity().writeTo(out);
			if(out.toString().contains("AHB-ACCEPT") == true)
				return "[ACCEPTED]";
			else if(out.toString().contains("AHB-REJECTED") == true)
				return "[REJECTED]";
		}
		catch (Exception ex)
		{
			MessageBox(ex.toString());
			return "[SERVER NOT FOUND]";
		}
		
		return "[UNKNOWN ERROR]";
	}
	
	public View.OnClickListener btnUpload_OnClick = new View.OnClickListener() {
		public void onClick(View v) {
			
			
			
			//String[] Files = fileList();
			
			TextView txtUpload = (TextView)findViewById(R.id.edtSummary);
			txtUpload.append("\nAttemting to upload files...\n");
			for(int x = 0; x <  Files.size(); x++)
	        {
				String status = "";
	        	txtUpload.append("Uploading File '" +  Files.get(x) + "'...");
	        	status = UploadFile(Files.get(x));
	        	txtUpload.append(status + "\n");
	        }
	        
			
			
	        
		}
	};
	public void MessageBox(String message){
	    Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
	}

}
